﻿using System.Data.Entity;
using Microsoft.AspNet.Identity.EntityFramework;
using RealtReview.DataAccess.Entities;
using RealtReview.DataAccess.Entities.IdentityProvider;
using RealtReview.DataAccess.Mappings;

namespace RealtReview.DataAccess
{
    public class RealtReviewContext : IdentityDbContext<ApplicationUser>
    {
        public RealtReviewContext()
            : base("name=RealtReviewContext", throwIfV1Schema: false)
        {
        }
        public static RealtReviewContext Create()
        {
            return new RealtReviewContext();
        }

        public virtual DbSet<Address> Addreses { get; set; }

        public virtual DbSet<Apartment> Apartments { get; set; }

        public virtual DbSet<Review> Reviews { get; set; }

        public virtual DbSet<Comment> Comments { get; set; }

        public virtual DbSet<Message> Messages { get; set; }

        public virtual DbSet<UserDetails> UserSettings { get; set; }
        public virtual DbSet<News> News { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new AspNetRolesMap());
            modelBuilder.Configurations.Add(new AspNetUsersMap());
            modelBuilder.Configurations.Add(new AddressMap());
            modelBuilder.Configurations.Add(new ApartmentMap());
            modelBuilder.Configurations.Add(new ReviewMap());
            modelBuilder.Configurations.Add(new CommentMap());
            modelBuilder.Configurations.Add(new MessageMap());
            modelBuilder.Configurations.Add(new NewsMap());
            modelBuilder.Configurations.Add(new UserDetailsMap());
            modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
            modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
            modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
        }
    }
}